<template>
  <div class="page-container">
    <h1 class="page-title neon-text">环境管理</h1>

    <el-card style="margin-bottom: 20px">
      <el-button type="primary" @click="showDialog()">
        <el-icon><Plus /></el-icon> 添加环境
      </el-button>
    </el-card>

    <el-card>
      <el-table :data="list" v-loading="loading">
        <el-table-column prop="name" label="环境名" />
        <el-table-column prop="env_type" label="环境类型">
          <template #default="{ row }">
            <el-tag :type="getEnvTypeTag(row.env_type)">
              {{ getEnvTypeText(row.env_type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="showDialog(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteItem(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="环境名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="环境类型">
          <el-select v-model="form.env_type" style="width: 100%">
            <el-option label="开发环境" value="dev" />
            <el-option label="测试环境" value="test" />
            <el-option label="预发布环境" value="staging" />
            <el-option label="生产环境" value="prod" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { environmentAPI } from '@/api'
import { ElMessage, ElMessageBox } from 'element-plus'

const loading = ref(false)
const list = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('添加环境')
const form = ref({ name: '', env_type: 'dev', description: '' })
const editId = ref(null)

const loadList = async () => {
  loading.value = true
  try {
    const res = await environmentAPI.getList()
    list.value = res.results || res
  } catch (error) {
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

const showDialog = (row = null) => {
  if (row) {
    dialogTitle.value = '编辑环境'
    editId.value = row.id
    form.value = { name: row.name, env_type: row.env_type, description: row.description || '' }
  } else {
    dialogTitle.value = '添加环境'
    editId.value = null
    form.value = { name: '', env_type: 'dev', description: '' }
  }
  dialogVisible.value = true
}

const submitForm = async () => {
  try {
    if (editId.value) {
      await environmentAPI.update(editId.value, form.value)
      ElMessage.success('更新成功')
    } else {
      await environmentAPI.create(form.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    loadList()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

const deleteItem = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除吗？', '警告', { type: 'warning' })
    await environmentAPI.delete(id)
    ElMessage.success('删除成功')
    loadList()
  } catch (error) {
    if (error !== 'cancel') ElMessage.error('删除失败')
  }
}

const getEnvTypeText = (type) => {
  const map = { dev: '开发环境', test: '测试环境', staging: '预发布环境', prod: '生产环境' }
  return map[type] || type
}

const getEnvTypeTag = (type) => {
  const map = { dev: '', test: 'success', staging: 'warning', prod: 'danger' }
  return map[type] || ''
}

onMounted(() => loadList())
</script>

<style scoped>
.page-container {
  padding: 20px;
}

.page-title {
  font-size: 32px;
  margin-bottom: 30px;
  color: var(--cyber-primary);
  font-family: 'Courier New', monospace;
}
</style>
